package com.zengyan.gd.utils;

import com.zengyan.gd.core.ServiceException;

import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

public class ImageUtils {
    public static ImageType getImageType(String type) {
        ImageType imageType;
        switch (type) {
            case "userAvatar":
                imageType = ImageType.userAvatar;
                break;
            case "userBg":
                imageType = ImageType.userBg;
                break;
            case "orgAvater":
                imageType = ImageType.orgAvater;
                break;
            case "orgBg":
                imageType = ImageType.orgBg;
                break;
            case "eventBg":
                imageType = ImageType.eventBg;
            default:
                throw new ServiceException("上传类型传错");
        }
        return imageType;
    }

    /**
     * 图片存储的地址
     */
    public final static String ImagePath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/";

    public static void isImageOk(MultipartFile file) {
        String suffix = FileUtils.getSuffix(file);
        switch (suffix) {
            case "png":
            case "jpg":
                return;
        }
        throw new ServiceException("图片格式不符合要求，只能是「png」,「jpg」格式。");
    }

    interface ImageCallBack{
        String provideImageName();
    }

    private static File createImage(ImageCallBack imageCallBack, MultipartFile file) throws Exception {
        String suffix = FileUtils.getSuffix(file);
        String path = "";
        path =  FileUtils.getfilePath(imageCallBack.provideImageName(),suffix);
        File dest = new File(path);
        return dest;
    }

    public static String saveUserImage(MultipartFile file, ImageType imageType,String stunum) throws Exception {

        FileUtils.isEmply(file);
        //获取文件名
        String fileName = file.getOriginalFilename();
        //验证文件名是否合格
        ImageUtils.isImageOk(file);
        //文件路径
        File dest =createImage(() -> stunum + "_"+imageType.toString(),file);
        // 检测是否存在目录
        FileUtils.isParentExists(dest);
        file.transferTo(dest);

        return dest.getName();
    }



    public static String saveOrgImage(MultipartFile file, ImageType imageType,int orgId) throws Exception {
        FileUtils.isEmply(file);
        //获取文件名
        String fileName = file.getOriginalFilename();
        //验证文件名是否合格
        ImageUtils.isImageOk(file);
        //文件路径
        File dest =createImage(() -> orgId + "_"+imageType.toString(),file);
        // 检测是否存在目录
        FileUtils.isParentExists(dest);
        file.transferTo(dest);
        return dest.getName();
    }


    public static String saveEventImage(MultipartFile file,int event_id) throws Exception {
        FileUtils.isEmply(file);
        //获取文件名
        String fileName = file.getOriginalFilename();
        //验证文件名是否合格
        ImageUtils.isImageOk(file);
        //文件路径
        File dest =createImage(() -> event_id + "_"+ImageType.eventBg,file);
        // 检测是否存在目录
        FileUtils.isParentExists(dest);
        file.transferTo(dest);
        return dest.getName();
    }





}
